Page({
  data: {
    //手机号
    phone: '',
    //手机号防止多次点击
    button: false,
    //发送验证码按钮初始内容
    text: '发送验证码'
  },
  phone(e) {
    let phone = e.detail.value;
    this.setData({
      phone: phone
    })
  },
  setCode(e) {
    let that = this;
    //data里取出之前存入的手机号
    let phone = that.data.phone;

    //验证手机号格式是否正确
    var phone_reg = /^1[35789]\d{9}$/;
    if (!phone_reg.test(phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: "none"
      })
      return;
    };
    //发送接口
    wx.request({
      url: 'https://636c-cloud1-2gyb5gh7c205cf9a-1323871060.tcb.qcloud.la',
      header: {
        'token': wx.getStorageSync('token')
      },
      data: {
        phone: phone
      },
      dataType: 'json',
      method: "POST",
      success(res) {
        //获取返回来的验证码
        console.log(res.data)
        let time = 60;
        setInterval(function () {
          time--;
          if (time > 0) {
            that.setData({
              text: time,
              button: true
            })
          } else {
            that.setData({
              text: '发送验证码',
              button: false
            })
          }
        }, 1000);
      }
    })
  },
})